package com.sakila.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sakila.dao.PaymentDao;
import com.sakila.pojo.Member;
import com.sakila.pojo.Payment;
import com.sakila.pojo.Rental;
import com.sakila.pojo.other.Page;
import com.sakila.service.PaymentService;
import com.sakila.vo.PaymentVo;

@Service
public class PaymentServiceImpl implements PaymentService {
	@Resource
	private PaymentDao paymentDao;

	@Override
	public PaymentVo findAll(Page page) {
		List<Payment> list = paymentDao.findAll(page);
		Long totalNumOfRows = paymentDao.getTotalNumOfRows();
		return new PaymentVo(list, totalNumOfRows);
	}

	@Override
	public PaymentVo findUnReturned(Page page) {
		List<Payment> list = paymentDao.findUnreturned(page, null);
		Long totalNumOfRows = paymentDao.getTotalNumOfRowsByUnreturned(null);
		return new PaymentVo(list, totalNumOfRows);
	}
	
	@Override
	public PaymentVo findUnReturned(Page page, Member member) {
		List<Payment> list = paymentDao.findUnreturned(page, member);
		Long totalNumOfRows = paymentDao.getTotalNumOfRowsByUnreturned(member);
		return new PaymentVo(list, totalNumOfRows);
	}

	@Override
	public PaymentVo findByMember(Page page, Member member) {
		Payment p = new Payment();
		p.setCustomer(member);
		List<Payment> list = paymentDao.findByProperties(page, p);
		Long totalNumOfRows = paymentDao.getTotalNumOfRowsByCustomer(member);
		return new PaymentVo(list, totalNumOfRows);
	}

	@Override
	public PaymentVo findByPeriod(Page page, Member member, Date startDate, Date endDate) {
		List<Payment> list = paymentDao.findByPeriod(page, member, startDate, endDate);
		Long totalNumOfRows = paymentDao.getTotalNumOfRowsByPeriod(member, startDate, endDate);
		return new PaymentVo(list, totalNumOfRows);
	}

	@Override
	public void finishOrder(Integer orderId) {
		Payment payment = paymentDao.findByPk(orderId);
		Rental rental = payment.getRental();
		rental.setReturnDate(new Date());
		paymentDao.updateRental(rental);
	}

	@Override
	public Payment findById(Integer id) {
		return paymentDao.findByPk(id);
	}
}
